//
//  DDChargeValueCell.m
//  DingDongZP
//
//  Created by weily on 16/5/11.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDChargeValueCell.h"

///	点击的界面
@interface _DDChargeSelectView : UIControl
@property (nonatomic, assign) BOOL isSelected;

@end

@implementation _DDChargeSelectView

- (void)setIsSelected:(BOOL)isSelected
{
    if (_isSelected != isSelected) {
        _isSelected = isSelected;
        
        if (isSelected) {
            self.layer.borderColor = [UIColor redColor].CGColor;
            self.layer.borderWidth = 1.0;
        }
        else {
            self.layer.borderColor = NULL;
            self.layer.borderWidth = 0;
        }
    }
}

@end

@interface _DDChargeValueSelectView : _DDChargeSelectView
{
    UILabel *_valueLab;
}
@end

@implementation _DDChargeValueSelectView

- (instancetype)initWithFrame:(CGRect)frame value:(NSInteger)value
{
    if (self = [super initWithFrame:frame]) {
        _valueLab = [[UILabel alloc] initWithFrame:CGRectZero];
        _valueLab.font = [UIFont systemFontOfSize:15.0];
        _valueLab.textColor = [UIColor blackColor];
        _valueLab.text = [NSString stringWithFormat:@"%ld元", value];
        NSString *valuestring = [NSString stringWithFormat:@"%ld元", value];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:valuestring];
        [string setAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20]} range:NSMakeRange(0, valuestring.length-1)];
        [string setAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.0]} range:NSMakeRange(valuestring.length-1, 1)];
        _valueLab.attributedText = string;
        [self addSubview:_valueLab];
        [_valueLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    }
    return self;
}

- (void)setIsSelected:(BOOL)isSelected
{
    [super setIsSelected:isSelected];
    if (isSelected) {
        _valueLab.textColor = [UIColor redColor];
    }
    else {
        _valueLab.textColor = [UIColor darkGrayColor];
    }
}

@end

@interface _DDChargeInsertSelectView : _DDChargeSelectView
<UITextFieldDelegate>
{
    UITextField *_insertField;
    /// 阻止开始输入
    UIView *_mask;
}
@end

@implementation _DDChargeInsertSelectView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _insertField = [[UITextField alloc] initWithFrame:CGRectZero];
        _insertField.placeholder = @"其他金额";
        _insertField.font = [UIFont systemFontOfSize:18.0];
        _insertField.textAlignment = NSTextAlignmentCenter;
        _insertField.enabled = NO;
        _insertField.delegate = self;
        [self addSubview:_insertField];
        [_insertField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@30);
            make.width.equalTo(self);
            make.center.equalTo(self);
        }];
        
    }
    return self;
}

- (void)setIsSelected:(BOOL)isSelected
{
    [super setIsSelected:isSelected];
    if (isSelected) {
        
        _insertField.enabled = YES;
        [_insertField becomeFirstResponder];
    }
    else {
        [_insertField resignFirstResponder];
        _insertField.enabled = NO;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end

@interface DDChargeValueCell ()
{
    NSArray *_valueViews;
}

@end

@implementation DDChargeValueCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        NSMutableArray *views = [NSMutableArray new];
        NSArray *values = @[@20, @50, @100, @200, @500];
        for (int i = 0; i < 5; i++) {
            _DDChargeValueSelectView *view = [[_DDChargeValueSelectView alloc] initWithFrame:CGRectZero value:[values[i] integerValue]];
            view.backgroundColor = [UIColor whiteColor];
            [view addTarget:self action:@selector(actionSelect:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:view];
            [views addObject:view];
        }
        
        _DDChargeInsertSelectView *insert = [[_DDChargeInsertSelectView alloc] initWithFrame:CGRectZero];
        insert.backgroundColor = [UIColor whiteColor];
        [insert addTarget:self action:@selector(actionSelect:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:insert];
        [views addObject:insert];
        
        _valueViews = views;
    }
    return self;
}

- (void)actionSelect:(id)sender
{
    _DDChargeSelectView *control = sender;
    if (!control.isSelected) {
        [_valueViews makeObjectsPerformSelector:@selector(setIsSelected:) withObject:@(NO)];
        control.isSelected = YES;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    float x = 5;
    float space = 3;
    float width = (self.width - x*2 - space*2) / 3;
    float height = (self.height - space) / 2;
    float y = 0;
    for (int i = 0; i < _valueViews.count; i++) {
        UIView *view = _valueViews[i];
        view.frame = CGRectMake(x, y, width, height);
        
        x += space + width;
        if (i % 3 == 2) {
            x = 5;
            y += space + height;
        }
    }
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



@end
